// pages/photograph.js
var proof = require('../../utils/proof.js');
var constract = require('../../utils/constract.js');
var comm = require('../../utils/common.js');
var app = getApp();

/**
 * 
 * @param {Array} _type   拍照上传or相册选择 
 * @param {Object} that   对this的保留 
 * @param {string} url    图片上传识别的接口 
 * @param {string} pages  那个页面调用这个方法 (card photograph)
 */
function uploadImg(_type, that, url, pages,uploadType){
  app.Verification(function (token) {
    wx.request({
      url: constract.index,
      header: { token: token },
      success: function (res) {
        var userInfo = res.data.data;
        if (userInfo.state == 0) {
          that.setData({
            // promptShow: app.globalData.globalAuth, //判断是否授权
            promptShow: true, //判断是否授权
          })
        } else {
          comm.ocrImg(_type,that,url,pages,uploadType);
        }
      }
    });
  })
}

Page({

  data: {
    show: true,
    noAuth: false,
  },

  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var abc = res.system.substr(0, 3);
        if (abc == "iOS") {
          that.setData({
            flag: true
          })
        }
      },
    });
    if(wx.getStorageSync('noAuth')){
      that.setData({
        noAuth: wx.getStorageSync('noAuth')
      })
      return;
    }
    app.Verification(function (token) {
      wx.request({
        url: constract.index,
        header: { token: token },
        success: function (res) {
          console.log(res);

          if(res.data.code === '23000') {
            that.setData({
              noAuth: true
            })
            return;
          } else {
            that.setData({
              noAuth: false
            })
            wx.removeStorageSync('noAuth');
          }

          var userInfo = res.data.data;
          that.setData({
            userInfo: userInfo
          })
          console.log(that.data);

          if (userInfo.state == 0) {
            wx.request({
              url: constract.defaultName, //查看首页的名片信息
              header: { token: token },
              success: function (res) {
                console.log(res);

                userInfo.name=res.data.name;
                userInfo.avatar = res.data.avatar;

                app.globalData.nickName = res.data.name;
                app.globalData.avatarUrl = res.data.avatar;
                
                that.setData({
                  userInfo: userInfo
                })
              }
            })
            that.setData({
              promptShow: true, //判断是否授权
            })  
          } 

        }
      });
    })
  },

  onHide: function() {
    if (wx.hideLoading) {
      wx.hideLoading();
    }
    this.setData({
      show: true
    })
  },

  takeUpload: function () {
    var that = this;

    uploadImg(['camera'],that, constract.scanCard,'photograph',1); //名片扫描->1
    
  },

  choseUpload: function () {
    uploadImg(['album'], this, constract.scanCard, 'photograph',1); //名片扫描->1
  },

  choseWxUpload: function(){
    uploadImg(['album'], this, constract.wxScan, 'photograph', 2); //微信截图扫描->2
  },

  mulAdd: function(){
    wx.navigateTo({
      url: '/pages/input-card/input-card?mInput=1&uploadType=0',
    })
  },

  promptCancel: function(){
    this.setData({
      promptShow: false
    })
  },
  hideAuth: function(){
    this.setData({
      getAuth: false
    })
  },
  onGotUserInfo: function(e){//允许
    if(e.detail.errMsg === "getUserInfo:fail auth deny") {
      this.setData({
        getAuth: true
      })
    } else {
      wx.removeStorageSync('noAuth');
      wx.navigateTo({
        url: `/pages/add-card/add-card?avatar=${e.detail.userInfo.avatarUrl}&name=${e.detail.userInfo.nickName}`
      })
    }
  }
})